﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;

namespace Xejen.Communication.Socket
{
    /// <summary>
    /// 表示Socket请求结果
    /// <para>
    /// 使用<see langword="byte[]"/>类型作为返回的数据结果类型，是为了适用所有数据类型，这样可以做到最通用的效果
    /// </para>
    /// </summary>
    /// <creator>marc</creator>
    public class SocketResponse : SocketResponse<byte[]>
    {
        /// <inheritdoc cref="SocketResponse{T}"/>
        public SocketResponse()
        {

        }

        /// <inheritdoc cref="SocketResponse{T}"/>
        /// <param name="encoding"><inheritdoc cref="Encoding" path="/summary"/></param>
        public SocketResponse(Encoding encoding = default) : base()
        {

        }

        /// <summary>
        /// 返回一个表示成功的<see cref="SocketResponse{T}"/>对象
        /// </summary>
        /// <param name="returnData"><inheritdoc cref="Result{T}.ReturnData" path="/summary"/></param>
        /// <param name="encoding"><inheritdoc cref="Encoding" path="/summary"/></param>
        public static new SocketResponse Success(byte[] returnData, Encoding encoding = default)
        {
            SocketResponse response = new SocketResponse(encoding);
            response.ReturnData = returnData;
            response.Code = SuccessCode;

            return response;
        }

        /// <summary>
        /// 返回一个表示失败的<see cref="SocketResponse{T}"/>对象
        /// </summary>
        /// <param name="code"><inheritdoc cref="IResult.Code" path="/summary"/></param>
        /// <param name="exception"><inheritdoc cref="IResult.Exception" path="/summary"/></param>
        public static new SocketResponse Fail(int code, Exception exception = default)
        {
            return new SocketResponse()
            {
                Code = code,
                Exception = exception,
            };
        }

        /// <inheritdoc/>
        public override string ToString()
        {
            if (this.ReturnData == null)
            {
                return null;
            }

            return Encoding.GetString(this.ReturnData);
        }
    }
}
